<template>
  <!-- 菜谱收藏 -->
    <div class="Collection">
      <div class="top" v-if="newlist==''">
        <div class="top-no-img">
          <img src="../assets/img/no_text.png" alt="">
          <span>暂无收藏</span>
        </div>
        <div>
          <span>菜谱推荐</span>
        </div>
        <div class="Collection-box">
                <div class="box" v-for="(item, index) in list" :key="item.id">
            <router-link :to="{ path: '/Caidetails', query: { id: item.id } }">
              <div class="top-img">
                <img class="cai_img" :src="item.image" alt="" />
              </div>
            </router-link>
            <div class="but">
              <router-link :to="{ path: '/Caidetails', query: { id: item.id } }">
                <span class="title"> {{ item.title }} </span>
              </router-link>
              <div class="but-user">
                <div class="but-user-name">
                  <img class="ren_img" :src="item.userimg" alt="" />
                  <span class="user">{{ item.username }}</span>
                </div>
                <div class="love-box">
                  
                </div>
              </div>
            </div>
          </div>
          <div class="kong"></div>
        </div>
      </div>


        <div class="box" v-for="(item, index) in newlist" :key="index" >
      <router-link :to="{ path: '/Caidetails', query: { id: item.arr.id } }">
        <div class="top-img">
          <img class="cai_img" :src="item.arr.image" alt="" />
        </div>
      </router-link>
      <div class="but">
        <router-link :to="{ path: '/Caidetails', query: { id: item.arr.id } }">
          <span class="title"> {{ item.arr.title }} </span>
        </router-link>
        <div class="but-user">
          <div class="but-user-name">
            <img class="ren_img" :src="item.arr.userimg" alt="" />
            <span class="user">{{ item.arr.username }}</span>
          </div>
          <div class="love-box">
            <img
              class="xing"
              v-if="item.arr.topent"
              src="../assets/img/xing_on.png"
              @click="goto(index)"
            />
            <img
              class="xing"
              v-else
              src="../assets/img/xing_no.png"
              alt=""
              @click="goto(index)"
            />
            <span>{{ item.arr.love }}</span>
          </div>
        </div>
      </div>
    </div>
    </div>
</template>


<script>
import { getrecommend } from "../api/index";
export default {
    data(){
        return{
            newlist:[],
            list: [],

        }
    },
    methods:{
      goto(index) {
      let str = localStorage.getItem("list");
            let Arr = [];
            console.log(str);
            if (str) {
              Arr = JSON.parse(str);
            } else {
              Arr = [];
            }
            let arr = this.newlist[index];

      if (this.newlist[index].arr.topent === false) {
        this.newlist[index].arr.topent = true;
        this.newlist[index].arr.love = this.newlist[index].arr.love + 1;
        let entry = {
          id:arr.id,
          arr
        };
      
        Arr.push(entry);
          console.log(Arr);
          
        localStorage.setItem("list", JSON.stringify(Arr));
      } else {
        this.newlist[index].arr.topent = false;
        this.newlist[index].arr.love = this.newlist[index].arr.love - 1;
        let arrto = Arr.filter(item=>{
          return item.id!=arr.id
        })
        Arr=arrto
        localStorage.setItem("list",JSON.stringify(Arr))
      }
    },
    },
    async created(){
        let list= localStorage.getItem("list")
        list =JSON.parse(list)
        this.newlist = list
        console.log(list);
        




    await getrecommend().then((err) => {
      let {
        state,
        result: { list },
      } = err;
      console.log(err);
      if (state == "success") {
        let rlist = list.filter((i) => {
          return i.type == 1;
        });
        console.log(rlist);
        let nlist = list.filter((i) => {
          return i.type == 3;
        });
        console.log(nlist);
        let arr = rlist.map((item) => {
          return {
            username: item?.r?.a?.n,
            userimg: item?.r?.a?.p,
            love: item?.r?.fc,
            title: item?.r?.n,
            image: item?.r?.img,
            id: item?.r?.id,
            topent: false,
          };
        });
        let Arr = nlist.map((item) => {
          return {
            username: item?.note?.author?.n,
            userimg: item?.note?.author?.p,
            love: item?.note?.like_count,
            title: item?.note?.title,
            image: item?.note?.image_u,
            id: item?.note?.id,
            inopent: false,
          };
        });
        this.list = arr;
        this.nelist = Arr;
      }
    });
  },
    }


</script>

<style lang="less" scoped>
@import "../assets/gong.less";
.top-no-img{
  width: 100%;
  height: 200px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  img{
    width: 200px;
    height: 200px;
  }
}
.Collection-box{
  display: flex;
flex-wrap: wrap;
}
.top{
  width: 100%;
}
.kong {
  width: 100%;
  height: 40px;
}
.Collection {
  background-color: rgb(241, 248, 248);
  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 45%;
  height: 290px;
  margin: 10px 2%;
  border-radius: 10px;
  background-color: #fff;
  .top-img {
    .cai_img {
      width: 100%;
      height: 190px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
    }
  }
  .but {
    width: 100%;
    height: 30%;
    padding: 5px;
    .title {
      display: block;
      width: 100%;
      height: 60%;
    }
    span {
      font-size: @zhufont;
    }
    .love-box {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 30%;
      span {
        margin-top: 5px;
      }
    }
    .but-user {
      .xing {
        width: 20px;
        height: 20px;
      }
      .user {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      span {
        font-size: 11px;
      }
      .ren_img {
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
      .but-user-name {
        width: 60%;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        span {
          margin-left: 5px;
        }
      }
      display: flex;
      line-height: 30px;
      align-items: center;
      justify-content: space-between;
    }
  }
}

</style>